package cn.easybuy.service.proudct;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import cn.easybuy.dao.product.ProductDao;
import cn.easybuy.dao.product.ProductDaoImpl;
import cn.easybuy.entity.Product;
import cn.easybuy.param.ProductParam;
import cn.easybuy.utils.DataSourceUtil;

public class ProductServiceImpl implements ProductService {

	private Connection conn;
	private ProductDao pDao;
	
	@Override
	public List<Product> queryProductList(ProductParam params) {
		List<Product> pList = new ArrayList<Product>();
		try {
			conn = DataSourceUtil.openConnection();
			pDao = new ProductDaoImpl(conn);
			pList = pDao.queryProductList(params);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DataSourceUtil.closeConnection(conn);
		}
		
		return pList;
	}

	@Override
	public int queryProductCount(ProductParam params) {
		Integer num = 0;
		try {
			conn = DataSourceUtil.openConnection();
			pDao = new ProductDaoImpl(conn);
			num = pDao.queryProductCount(params);
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		} finally {
			DataSourceUtil.closeConnection(conn);
		}
		return num;
	}

	@Override
	public Product findById(String id) {
		
		Connection connection = null;
        Product product = null;
        try {
            connection = DataSourceUtil.openConnection();
            ProductDao productDao = new ProductDaoImpl(connection);
            product = productDao.getProductById(Integer.parseInt(id));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DataSourceUtil.closeConnection(connection);
        }
        return product;
	}

}
